﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02_委托的必要性_2
{
    public delegate double MathDelegate(int a, int b);

    internal class Program
    {
        static void Main(string[] args)
        {

            int leftValue = 10;
            int rightValue = 20;

            //计算结果
            double result = DoMath(leftValue, rightValue, "+");
            Console.WriteLine("加法结果：" + result);

            result = DoMath(leftValue, rightValue, "-");
            Console.WriteLine("减法结果：" + result);

            result = DoMath(leftValue, rightValue, "*");
            Console.WriteLine("乘法结果：" + result);

            result = DoMath(leftValue, rightValue, "/");
            Console.WriteLine("除法结果：" + result);

            result = DoMath(leftValue, rightValue, "平方和");
            Console.WriteLine("平方和结果：" + result);

            result = DoMath(leftValue, rightValue, "立方和");
            Console.WriteLine("立方和结果：" + result);


            Console.WriteLine("===============================================");

            result = Domath(leftValue, rightValue, new MathDelegate(Add));
            Console.WriteLine("加法结果：" + result);

            result = Domath(leftValue, rightValue, new MathDelegate(Sub));
            Console.WriteLine("减法结果：" + result);

            result = Domath(leftValue, rightValue, new MathDelegate(Mul));
            Console.WriteLine("乘法结果：" + result);

            result = Domath(leftValue, rightValue, new MathDelegate(Div));
            Console.WriteLine("除法结果：" + result);

            result = Domath(leftValue, rightValue, SquareSum);
            Console.WriteLine("平方和结果：" + result);

            result = Domath(leftValue, rightValue, CubeSum);
            Console.WriteLine("立方和结果：" + result);



            Console.Read();

        }

        /// <summary>
        /// 计算方法
        /// </summary>
        /// <param name="a">左操作数</param>
        /// <param name="b">右操作数</param>
        /// <param name="type">运算符</param>
        /// <returns></returns>
        public static double DoMath(int a, int b, string type)
        {
            //计算结果
            double result = 0;
            //判断不同的运算符执行不同的计算
            switch (type)
            {
                case "+":
                    result = Add(a,b);
                    break;
                case "-":
                    result = Sub(a, b);
                    break;
                case "*":
                    result = Mul(a, b);
                    break;
                case "/":
                    result = Div(a, b);
                    break;
                case "平方和":
                    result = SquareSum(a, b);
                    break;
                case "立方和":
                    result = CubeSum(a, b);
                    break;
            }
            //返回结构
            return result;

        }

        public static double Domath(int a, int b, MathDelegate mathDelegate)
        {
            return mathDelegate.Invoke(a, b);
        }


        public static double Add(int a, int b)
        {
            return a + b;
        }

        public static double Sub(int a, int b)
        {
            return a - b;
        }

        public static double Mul(int a, int b)
        {
            return a * b;
        }

        public static double Div(int a, int b)
        {
            return a * 1.0 / b;
        }

        public static double SquareSum(int a, int b)
        {
            return a * a + b * b;
        }

        public static double CubeSum(int a, int b)
        {
            return a * a * a + b * b * b;
        }




    }
}
